package qc.module.ehs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.exception.QCPromptException;
import qc.module.ehs.dto.camera.CameraDto;
import qc.module.ehs.dto.camera.CameraQueryConditionDto;
import qc.module.ehs.entity.EhsCameraInfo;
import qc.module.ehs.mapper.EhsCameraInfoMapper;
import qc.module.ehs.repository.EhsCameraInfoRepository;

import java.util.List;

/**
 * EhsCameraInfoService
 *
 * @author QuCheng Tech
 * @since 2025/6/24
 */
@Service
public class EhsCameraInfoService {
    @Autowired
    private EhsCameraInfoRepository repository;

    /**
     * 查询摄像头信息
     *
     * @param condition 查询条件
     * @return java.util.List<qc.module.ehs.dto.camera.CameraDto>
     * @author QuCheng Tech
     * @since 2025/6/24
     */
    public List<CameraDto> query(CameraQueryConditionDto condition) throws QCPromptException {
        if (condition == null)
            throw new QCPromptException("查询条件不能为空");
        if (StringUtils.isBlank(condition.getOrgcode()))
            throw new QCPromptException("组织代码不能为空");

        LambdaQueryWrapper<EhsCameraInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EhsCameraInfo::getOrgcode, condition.getOrgcode());
        if (StringUtils.isNotBlank(condition.getUnitid())) {
            wrapper.eq(EhsCameraInfo::getUnitid, condition.getUnitid());
        }
        //关键字：可以在名称、位置中进行模糊匹配
        if (StringUtils.isNotBlank(condition.getKeywords())) {
            wrapper.and(q -> {
                q.like(EhsCameraInfo::getName, condition.getKeywords()).or()
                        .like(EhsCameraInfo::getLocation, condition.getKeywords());
            });
        }

        List<EhsCameraInfo> ens = repository.selectList(wrapper);
        if (ens != null && ens.size() > 0x0)
            return EhsCameraInfoMapper.MAPPER.toDtoList(ens);

        return null;
    }
}
